package com.example.kotlinExample.config

import com.google.gson.Gson
import org.springframework.data.redis.serializer.RedisSerializer

/**
 *
 *
 * @author liliang
 */
class Gson2JsonRedisSerializer<T>(var clazz: Class<T>): RedisSerializer<T> {

    private val gson = Gson()

    override fun serialize(t: T?): ByteArray? {
        return if (t == null) {
            ByteArray(0)
        } else {gson.toJson(t).toByteArray()}
    }

    override fun deserialize(bytes: ByteArray?): T? {
        return if (bytes == null || bytes.isEmpty()) {
            null
        } else {gson.fromJson(String(bytes), clazz)}
    }
}